<template>
  <el-dialog
    :visible.sync="dialogVisible"
    width="800px"
    :before-close="handleClose"
    :close-on-click-modal="false"
    append-to-body
    :title="title"
  >
    <el-form :model="form" ref="formRef" label-width="90px">
      <el-form-item
        label="病种名称"
        prop="disease_name"
        :rules="[{ required: true, message: '请输入病种', trigger: 'blur' }]"
      >
        <el-input placeholder="请输入" v-model="form.disease_name"></el-input>
      </el-form-item>
      <el-form-item
        label="会诊收费"
        prop="price"
        :rules="[
          { required: true, message: '请输入会诊收费', trigger: 'blur' },
        ]"
      >
        <el-input
          placeholder="请输入"
          type="number"
          v-model="form.price"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="牵头医生"
        prop="doctor_id"
        :rules="[
          { required: true, message: '请输入会诊收费', trigger: 'blur' },
        ]"
      >
        <el-select
          v-model="form.doctor_id"
          size="medium"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in doctorArr"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="tx-c">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="confirm" :loading="btnLoading"
        >确认</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { debounce } from "@/util";
import { ApiDiseaseListEdit, ApiDiseaseListAdd } from "@/api/index.js";
import ImgUploadPath from "@/components/imgUpload-path.vue";
import { validatePhone } from "@/util/validate";
export default {
  props: ["roleData", "departmentData", "doctorArr"],
  components: {
    ImgUploadPath,
  },
  data() {
    return {
      validatePhone,
      ItemArr: [{ value1: "", value2: "" }],
      oldform: {
        disease_name: "",
        price: "",
        doctor_id: "",
      },
      form: {},
      btnLoading: false,
      dialogVisible: false,
      id: null,
      title: "",
      imgsDefalut: [],
      roleArr: [
        { name: "医师", code: "医师" },
        { name: "护师", code: "护师" },
      ],
    };
  },

  methods: {
    addM() {
      this.ItemArr.push({
        value1: "",
        value2: "",
      });
    },
    deleM() {
      if (this.ItemArr.length > 1) {
        this.ItemArr.pop({
          value1: "",
          value2: "",
        });
      } else {
        this.$alert("最后一个无法删除！", "提示信息", {
          confirmButtonText: "确定",
        });
      }
    },

    onUploadSuccess(path) {
      console.log("111", path);
      this.form.icon = path;
    },
    onUploadRemove() {
      this.form.icon = "";
    },
    confirm: debounce(
      function () {
        this.$refs["formRef"].validate(async (valid) => {
          if (valid) {
            this.btnLoading = true;
            try {
              //时间段转换
              console.log("123111", this.ItemArr);
              if (this.ItemArr.length > 0) {
                this.form.time_range =
                  this.ItemArr[0].value1 + "-" + this.ItemArr[0].value2;
              }

              const form = {
                ...this.form,
              };
              if (this.id) {
                await ApiDiseaseListEdit(this.id, form);
                this.$message.success("修改成功");
              } else {
                await ApiDiseaseListAdd(form);
                this.$message.success("添加成功");
                /* this.$alert("添加成功，该员工初始密码为：123456", "提示", {
                  confirmButtonText: "确定",
                  callback: (action) => {},
                }); */
              }

              this.handleClose();
              this.$emit("refresh");
            } finally {
              this.btnLoading = false;
            }
          } else return false;
        });
      },
      500,
      true
    ),

    show(row) {
      const data = row || this.oldform;
      for (const key in this.oldform) {
        this.$set(this.form, key, data[key]);
      }

      if (row) {
        this.id = row.id;
        this.title = "编辑MDT会诊管理";
        //时间段
        if (row.time) {
          this.form.time = row.time.split(",");
        }
        //接诊时间段
        if (row.time_range) {
          let arr = row.time_range.split("-");
          this.ItemArr[0].value1 = arr[0];
          this.ItemArr[0].value2 = arr[1];
        }
        if (row.icon) this.imgsDefalut = [{ url: this.$path + row.icon }];
      } else {
        this.id = null;
        this.title = "添加MDT会诊管理";
      }
      this.dialogVisible = true;
    },

    handleClose() {
      this.dialogVisible = false;
      this.form = { ...this.oldform };
      this.ItemArr = [{ value1: "", value2: "" }];
      this.$nextTick((_) => {
        this.$refs.formRef.clearValidate();
        /*      this.$refs.ImgUploadPath.clear(); */
      });
    },
  },
};
</script>

<style></style>
